var tasker = require("async");
var fs = require("fs");
var {resolve} = require("path");

var stat = fs.statSync("hello.txt");  //文件的各种信息
console.log('stat:');
console.log(stat);

//将对象转换为字符串，写入文件
fs.writeFile("hello.txt", JSON.stringify({ name: "katy", age: 30 }), (err) => {
    if(fs.existsSync("hello.txt")){
        fs.readFile("hello.txt", (err, data) => {
            //Buffer 转为字符串
            console.log(data.toString());
        });
    }else{
        console.log("文件不存在".cyan);
    }
    // fs.exists('hello.txt', function (exist) {
    //     if (exist) {
    //         fs.readFile("hello.txt", (err, data) => {
    //             //Buffer 转为字符串
    //             console.log(data.toString());
    //         });
    //     } else {
    //         console.log("文件不存在".cyan);
    //     }
    // });
});

//循环查找，获取根目录下的所有文件,
console.log(__dirname);

var root = "E:/gitee/fis2-demo";
var list = [];

//遍历获得所有文件
function getFiles(path,parentPath){
    //首先要存在
    var curPath = path?resolve(parentPath,path):parentPath;
    if(fs.existsSync(curPath)){
        var stat = fs.statSync(curPath);
        if(stat.isDirectory()){
            // console.log(curPath);
            fs.readdirSync(curPath).forEach(ele => {
                // console.log(`ele: ${ele}, curPath:${curPath}`);
                getFiles(ele,curPath);
            });
        }else if(stat.isFile()){
            // console.log(`file: ${curPath}`);
            list.push(curPath);
        }
    }
}

getFiles('',root);
console.log(list);
